<?php

namespace app\admin\controller;

use think\worker\Server;

class Socket extends Server
{

    protected $socket = 'websocket://0.0.0.0:2346';

    //客户端信息入口
    public function onMessage($connection, $data)
    {
        global $worker;
//        echo $connection->id;
        //来源数据解析分割
        $param = json_decode($data, true);


    }

    // 当有客户端连接断开时
    public function onClose($connection)
    {
        global $worker;
        if (isset($connection->uid)) {
            // 连接断开时删除映射
            unset($worker->uidConnections[$connection->uid]);
        }

    }



    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [], $bomb = 0)
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "链接成功", "data" => $data, 'bomb' => $bomb],
            "ILLEGAL_REQUEST" => ["status" => 1, "msg" => "参数错误", "data" => $data, 'bomb' => $bomb],
            "LOGIN_EORRE" => ["status" => 2, "msg" => "登陆失败", "data" => $data, 'bomb' => $bomb],
            "USER_NOT_EXIST" => ["status" => 3, "msg" => "用户不存在", "data" => $data, 'bomb' => $bomb],
            "OFF_LINE" => ["status" => 4, "msg" => "对方暂时不在线", "data" => $data, 'bomb' => $bomb],
            "FRIENDS_MSG" => ["status" => 5, "msg" => "好友消息", "data" => $data, 'bomb' => $bomb],
            "MASS_MSG" => ["status" => 6, "msg" => "群发消息", "data" => $data, 'bomb' => $bomb],
            "LOGIN_SUCCESS" => ["status" => 7, "msg" => "登陆成功", "data" => $data, 'bomb' => $bomb],
            "SYSTEM_MSG" => ["status" => 8, "msg" => "系统消息", "data" => $data, 'bomb' => $bomb],
            "NEWUSER_MSG" => ["status" => 9, "msg" => "新用户加入", "data" => $data, 'bomb' => $bomb],
            "USER_ALERD" => ["status" => 10, "msg" => "该用户已经登陆", "data" => $data, 'bomb' => $bomb],
            "USER_LOGOUT" => ["status" => 11, "msg" => "用户离线", "data" => $data, 'bomb' => $bomb],
            "GROUP_MSG" => ["status" => 12, "msg" => "群组消息", "data" => $data, 'bomb' => $bomb],
            "GROUP_IN_SUCCESS" => ["status" => 13, "msg" => "进入聊天室成功", "data" => $data, 'bomb' => $bomb],
            "GROUP_OUT_SUCCESS" => ["status" => 14, "msg" => "退出聊天室成功", "data" => $data, 'bomb' => $bomb],
        ];
        return $maps[$key];
    }

}